
package com.myutil;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.myutil.oss.AliyunSSLCertService;
import com.myutil.qiniu.QiNiuSSLCertService;
import com.myutil.utils.ConfigFileUtils;

public class UpdateSSLCert {
	public static void main(String[] args) {
		//java -jar xx.jar -C aaa.conf
		
		// 定义阶段
		Options options = new Options();

		Option filePathOption = new Option("f", true, "配置文件路径");
		filePathOption.setRequired(false); // 文件路径为必传选项
		filePathOption.setArgName("configFilePath");
		
		Option modelOption = new Option("m", true, "all(更新全部七牛和阿里云的ssl证书),qiniu(只更新七牛的证书),oss(只更新阿里云的oss证书)");
		modelOption.setRequired(false); // 文件路径为必传选项
		modelOption.setArgName("model");
		
		//Option deleteOldOption = new Option("d", false, "删除旧证书");
		Option helpOption = new Option("h", "help", false, "帮助文档");

		options.addOption(filePathOption);
		options.addOption(modelOption);
		//options.addOption(daemonRunOption);
		options.addOption(helpOption);

		// 解析阶段
		CommandLineParser parser = new DefaultParser();
		CommandLine cmd;
		try {
			cmd = parser.parse(options, args);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
		
		// 获取文件路径
		if (cmd.hasOption(filePathOption)) {
			
			String model = cmd.getOptionValue(modelOption, "all");
			
			String filePath = cmd.getOptionValue(filePathOption);
			System.out.println("配置文件路径 : " + filePath);
			JSONObject config = ConfigFileUtils.readFileToJson(filePath);

			if(model.equals("qiniu")||model.equals("all")) {
				//cach 起来不影响更新oss证书
				try {
					System.out.println("\n==========更新qiniu证书===begin=========");
					JSONObject qiniu = config.getJSONObject("qiniu");

					String accessKey = qiniu.getString("accessKey");
					String secretKey = qiniu.getString("secretKey");
					QiNiuSSLCertService qiNiuUtil = new QiNiuSSLCertService(accessKey,secretKey);
					
					JSONArray updateList = qiniu.getJSONArray("updateList");
					for (int i = 0; i < updateList.size(); i++) {
						JSONObject update=updateList.getJSONObject(i);
						
						Boolean deleteOldCert = update.getBoolean("deleteOldCert");
						if (deleteOldCert == null) {
							deleteOldCert = false;
						}
						String sslCertName = update.getString("sslCertName");
						String sslCertPath = update.getString("sslCertPath");
						String sslPrivatekeyPath = update.getString("sslPrivatekeyPath");
						
						String[] domainNames = update.getString("domainNames").split(",");
						
						qiNiuUtil.updateSSLCert(domainNames,sslCertName,sslCertPath,sslPrivatekeyPath,deleteOldCert);
					}
					System.out.println("==========更新qiniu证书======end======\n");
					
					// 检测下证书 有没有快过期的证书
					qiNiuUtil.sendExpriedCertEmailNotify(config);
					
				}catch(Exception ex) {
					ex.printStackTrace();
				}
			}
			if(model.equals("oss")||model.equals("all")) {
				System.out.println("\n==========更新oss证书===begin=========");

				JSONObject oss = config.getJSONObject("oss");

				String accessKey = oss.getString("accessKey");
				String secretKey = oss.getString("secretKey");
				AliyunSSLCertService client = new AliyunSSLCertService(accessKey,secretKey);

				try {
					String[] domains = oss.getString("domainNames").split(",");
					
					client.autoRenewAliyunSSLCert(domains);
				}catch(Exception ex) {
					ex.printStackTrace();
				}
				System.out.println("==========更新oss证书======end======\n");
				// 检测下证书 有没有快过期的证书
				client.sendExpriedCertEmailNotify(config);
			}
			
			
			
		}else {
			//if (args.length == 0 || cmd.hasOption(helpOption)) {
				HelpFormatter formatter = new HelpFormatter();
				formatter.printHelp("java -jar xx.jar -f /data/ssl/qiniu.conf", options);
			//}
		}

		// 后台运行
		/*if (cmd.hasOption(daemonRunOption)) {
			System.out.println("后台运行");
		}*/
		
		
	}

}
